package com.example.cola.domain.model;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 用户聚合根
 * 
 * @author 资深开发专家
 * @version 1.0
 */
@Data
@NoArgsConstructor
public class User {
    
    private UserId id;
    private Email email;
    private PhoneNumber phoneNumber;
    private String name;
    private String password;
    private UserStatus status;
    private Boolean emailVerified;
    private Boolean phoneVerified;
    private LocalDateTime lastLoginTime;
    private String lastLoginIp;
    private LocalDateTime createdTime;
    private LocalDateTime updatedTime;
    
    /**
     * 创建新用户
     */
    public static User create(Email email, PhoneNumber phoneNumber, String name, String password) {
        User user = new User();
        user.email = email;
        user.phoneNumber = phoneNumber;
        user.name = name;
        user.password = password;
        user.status = UserStatus.ENABLED;
        user.emailVerified = false;
        user.phoneVerified = false;
        user.createdTime = LocalDateTime.now();
        user.updatedTime = LocalDateTime.now();
        return user;
    }
    
    /**
     * 更新用户信息
     */
    public void updateInfo(String name, PhoneNumber phoneNumber) {
        if (name != null && !name.trim().isEmpty()) {
            this.name = name.trim();
        }
        if (phoneNumber != null) {
            this.phoneNumber = phoneNumber;
        }
        this.updatedTime = LocalDateTime.now();
    }
    
    /**
     * 更新密码
     */
    public void updatePassword(String newPassword) {
        if (newPassword == null || newPassword.trim().isEmpty()) {
            throw new IllegalArgumentException("新密码不能为空");
        }
        this.password = newPassword;
        this.updatedTime = LocalDateTime.now();
    }
    
    /**
     * 启用用户
     */
    public void enable() {
        this.status = UserStatus.ENABLED;
        this.updatedTime = LocalDateTime.now();
    }
    
    /**
     * 禁用用户
     */
    public void disable() {
        this.status = UserStatus.DISABLED;
        this.updatedTime = LocalDateTime.now();
    }
    
    /**
     * 验证邮箱
     */
    public void verifyEmail() {
        this.emailVerified = true;
        this.updatedTime = LocalDateTime.now();
    }
    
    /**
     * 验证手机号
     */
    public void verifyPhone() {
        this.phoneVerified = true;
        this.updatedTime = LocalDateTime.now();
    }
    
    /**
     * 记录登录信息
     */
    public void recordLogin(String loginIp) {
        this.lastLoginTime = LocalDateTime.now();
        this.lastLoginIp = loginIp;
        this.updatedTime = LocalDateTime.now();
    }
    
    /**
     * 检查用户是否可用
     */
    public boolean isAvailable() {
        return status != null && status.isEnabled();
    }
    
    /**
     * 检查密码是否正确
     */
    public boolean checkPassword(String inputPassword) {
        return password != null && password.equals(inputPassword);
    }
    
    /**
     * 检查是否可以通过邮箱登录
     */
    public boolean canLoginByEmail() {
        return isAvailable() && email != null && emailVerified;
    }
    
    /**
     * 检查是否可以通过手机号登录
     */
    public boolean canLoginByPhone() {
        return isAvailable() && phoneNumber != null && phoneVerified;
    }
}
